<?php
class Admin_Emp_Room extends Admin_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('room_m');
		$this->check_permissions();
	}
	
	public function index(){
		$all = $this->room_m->get_all();
		$tmp = array();
		array_level1($all,$tmp);
		$model = array();
		foreach($tmp as $item){
			if(count($item)>0)
			$model[] = $item;
		}
		$this->template
			->set('action',$this->action)
				->set('model',$model)
			->build($this->controller.'/index');
	}
	
	public function create(){
		$model = $this->input->post();
		$skip = array('btnAction','id');
		$this->form_validation->set_rules('title', lang('room:title'), 'required');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				//print_r($model);exit;
				$skip = array('btnAction');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$id_insert = $this->room_m->insert($model);
				$this->redirect($this->method,$type,$id_insert);
			}
		}
		$rooms = $this->room_m->get_all();
		$room_select = array();
		array_level1($rooms,$room_select);
		$room_select = array_for_select($room_select,'id','title','frefix');
		$this->template
			->set('model',$model)
			->set('room_select',$room_select)
			->build($this->controller.'/form');
	}
	
	public function edit($id){
		$model = (array)$this->room_m->get($id);
		$this->form_validation->set_rules('title', lang('room:title'), 'required');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); 
					}
				}
				$this->room_m->update($id,$model);
				$this->redirect($this->method,$type,$id);
			}
		}
		$rooms = $this->room_m->get_all();
		$room_select = array();
		array_level1($rooms,$room_select);
		$room_select = array_for_select($room_select,'id','title','frefix');
		$this->template
			->set('model',$model)
			->set('room_select',$room_select)
			->build($this->controller.'/form');
	}
	
	public function delete($id){
		$this->room_m->delete($id);
		redirect('admin/emp_room');
	}
}